
var clampMode = 0; // 空间模式
/**
 * 矢量底图
 */
function vecMap() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    viewer.imageryLayers.removeAll();


     //矢量底图
    var vec = new Cesium.WebMapTileServiceImageryProvider({
        url:"http://t2.tianditu.com/vec_w/wmts?service=WMTS&tk=01ca247bd6af0c025bd16ba8778b602b",
        layer : 'vec',
        style : 'default',
        format : 'tiles',
        tileMatrixSetID : 'w',
        maximumLevel: 19
    });
    viewer.imageryLayers.addImageryProvider(vec);

    //天地图中文注记
    var zhuji=new Cesium.WebMapTileServiceImageryProvider({
        url:"http://t2.tianditu.com/cia_w/wmts?service=WMTS&tk=01ca247bd6af0c025bd16ba8778b602b",
        request: 'GetTile',
        layer: 'cia',
        style: 'default',
        format: 'tiles',
        tileMatrixSetID : 'w',
        tilematrix: 12

    });
    viewer.imageryLayers.addImageryProvider(zhuji);

}

/**
 * 影像底图
 */
function imgMap() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    viewer.imageryLayers.removeAll();

    //加载arcgis影像底图
    var img = new Cesium.ArcGisMapServerImageryProvider({
        url : 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
    });
    viewer.imageryLayers.addImageryProvider(img);
    //天地图中文注记
    var zhuji=new Cesium.WebMapTileServiceImageryProvider({
        url:"http://t2.tianditu.com/cia_w/wmts?service=WMTS&tk=01ca247bd6af0c025bd16ba8778b602b",
        request: 'GetTile',
        layer: 'cia',
        style: 'default',
        format: 'tiles',
        tileMatrixSetID : 'w',
        tilematrix: 12

    });
    viewer.imageryLayers.addImageryProvider(zhuji);

}

/**
 * 加载山东边界图
 */
function addShandongProvince() {

    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    //山东边界
    var url = "http://101.200.150.193:8090/iserver/services/map-provincetouming/rest/maps/provincetouming";
    provider = new Cesium.SuperMapImageryProvider({
        url : url
    });
    var imagery = viewer.imageryLayers.addImageryProvider(provider);
    imagery.alpha = 1.0;


}

/**
 * 加载非遗点
 */
function addfeiyiPosition() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    //非遗
    var url = "http://101.200.150.193:8090/iserver/services/map-feiwuzhi/rest/maps/feiwuzhi";
    provider = new Cesium.SuperMapImageryProvider({
        url : url
    });
    var imagery = viewer.imageryLayers.addImageryProvider(provider);
    imagery.alpha = 1.0;
    imagery.brightness=2.0;

}

/**
 * 移除当前屏幕所有图层
 */
function clearAllLayer() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    viewer.imageryLayers.removeAll();
}
/**
 * 加载图层和透明度控制
 */
function addLayers(Cesium) {
    var viewer=mapApp.cesiumMap.viewer;

    try{
        //添加S3M图层
        var scene = viewer.scene;

        var widget = viewer.cesiumWidget;
        viewer.scene.undergroundMode = true; //设置开启地下场景
        viewer.scene.screenSpaceCameraController.minimumZoomDistance = -1000;//设置相机最小缩放距离,距离地表-1000米
        viewer.scene.terrainProvider.isCreateSkirt = false; //关闭裙边



        // //添加地下管理模型
         var huangdao_underground=scene.open('http://47.104.21.51:8090/iserver/services/3D-XHA/rest/realspace');

        Cesium.when.all([huangdao_underground],function (layers) {

            //调整相机视角

            var rectangle = new Cesium.Rectangle.fromDegrees( 114.918965,33.669859,
                122.596,38.348);
            scene.camera.setView({
                destination: rectangle,
                orientation : {
                    heading : 0,
                    pitch : Cesium.Math.toRadians(-90),
                    roll :0.0
                }
            });



            //设置图层的阴影模式
            layers[0].selectEnabled = false;
            //shadowType：获取或设置图层中模型参与显示阴影的范围类型。
            layers[0].shadowType = 2;

            var viewModel = { //监听滑动条变化，改变alpha的值，设置地表透明度
                overGroundAlpha : 1
            };
            Cesium.knockout.track(viewModel);
            var toolbar = document.getElementById('toolbar');
            Cesium.knockout.applyBindings(viewModel, toolbar);

        },function(e){
            if (widget._showRenderLoopErrors) {
                var title = '加载SCP失败，请检查网络连接状态或者url地址是否正确？';
                widget.showErrorPanel(title, undefined, e);
            }
        });
    } catch(e){
        if (widget._showRenderLoopErrors) {
            var title = '渲染时发生错误，已停止渲染。';
            widget.showErrorPanel(title, undefined, e);
        }
    }
}


/**
 *加载专题图经济中心点
 * 王台经济区id： 1
 * 前湾港区id： 2
 * 董家口港区id: 3
 * 灵山湾港区id： 4
 */
function addZhuanTiPoint() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;


    var rectangle = new Cesium.Rectangle.fromDegrees(119.406965,35.178182,120.595063,35.81685);
    scene.camera.setView({
        destination: rectangle,
        orientation : {
            heading : 0,
            pitch : Cesium.Math.toRadians(-75),
            roll :0.0
        }
    });

    var ro=0;
    function callback(){
        ro=ro+0.03;
        return ro;
    }
    var wangtai=viewer.entities.add(
        {
            id: 1,
            position:Cesium.Cartesian3.fromDegrees(120.009575,36.085459  ,50.00000 ),
            ellipse:{
                semiMinorAxis:3000,
                semiMajorAxis:3000,
                height:100,
                material:new Cesium.ImageMaterialProperty({
                    image:'./img/Oval.png',
                    repeat:new Cesium.Cartesian2(1.0,1.0),
                    transparent:true
                }),
                rotation:new Cesium.CallbackProperty(callback,false),
                stRotation:new Cesium.CallbackProperty(callback,false),
                outline:false,
                numberOfVerticalLines:100
            } ,
            billboard:{
                image:'./img/王台国际城.png',
                width:200,
                height:200,

            }
        }
    );

    var qianwanggang=viewer.entities.add(
        {
            id: 2,
            position:Cesium.Cartesian3.fromDegrees(120.189706,36.004422 ,50.00000 ),
            ellipse:{
                semiMinorAxis:3000,
                semiMajorAxis:3000,
                height:100,
                material:new Cesium.ImageMaterialProperty({
                    image:'./img/Oval.png',
                    repeat:new Cesium.Cartesian2(1.0,1.0),
                    transparent:true
                }),
                rotation:new Cesium.CallbackProperty(callback,false),
                stRotation:new Cesium.CallbackProperty(callback,false),
                outline:false,
                numberOfVerticalLines:100
            },
            billboard:{
                image:'./img/前湾港区.png',
                width:200,
                height:200

            }
        }
    );

    var dongjiakou=viewer.entities.add(
        {
            id: 3,
            position:Cesium.Cartesian3.fromDegrees(119.77627,35.609025 ,50.00000 ),
            ellipse:{
                semiMinorAxis:3000,
                semiMajorAxis:3000,
                height:100,
                material:new Cesium.ImageMaterialProperty({
                    image:'./img/Oval.png',
                    repeat:new Cesium.Cartesian2(1.0,1.0),
                    transparent:true
                }),
                rotation:new Cesium.CallbackProperty(callback,false),
                stRotation:new Cesium.CallbackProperty(callback,false),
                outline:false,
                numberOfVerticalLines:100
            },
            billboard:{
                image:'./img/董家口港区.png',
                width:200,
                height:200

            }
        }
    );

    var lingshanwan=viewer.entities.add(
        {
            id: 4,
            position:Cesium.Cartesian3.fromDegrees(120.102831,35.917297 ,50.00000 ),
            ellipse:{
                semiMinorAxis:3000,
                semiMajorAxis:3000,
                height:100,
                material:new Cesium.ImageMaterialProperty({
                    image:'./img/Oval.png',
                    repeat:new Cesium.Cartesian2(1.0,1.0),
                    transparent:true
                }),
                rotation:new Cesium.CallbackProperty(callback,false),
                stRotation:new Cesium.CallbackProperty(callback,false),
                outline:false,
                numberOfVerticalLines:100
            },
            billboard:{
                image:'./img/灵山湾影视城.png',
                width:200,
                height:200

            }
        }
    )

}

/**
 * 清除专题图经济中心点
 */
function clearZhuantiPoint() {

    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    viewer.entities.removeById(1);
    viewer.entities.removeById(2);
    viewer.entities.removeById(3);
    viewer.entities.removeById(4);

}


/**
 * 加载专题图线状
 */
function addZhuantiMapLine() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    var rectangle = new Cesium.Rectangle.fromDegrees(119.406965,35.178182,120.595063,35.81685);
    scene.camera.setView({
        destination: rectangle,
        orientation : {
            heading : 0,
            pitch : Cesium.Math.toRadians(-75),
            roll :0.0
        }
    });
    //专题图线状
    mapApp.cesiumMap.viewer.scene=scene.addS3MTilesLayerByScp('http://47.104.21.51:8090/iserver/services/3D-QDMap/rest/realspace/datas/data2/config',{
        name: 'zhuantiLine'
    });

    var data2=mapApp.cesiumMap.viewer.scene.layers.find("zhuantiLine");

    if(data2){
        data2._visible=true;
    }
    //mapApp.cesiumMap.viewer.scene=scene.open('http://47.104.21.51:8090/iserver/services/3D-QDMap/rest/realspace');
}


/**
 * 加载专题图面状
 */

function addZhuantiMapPolyGon() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    var rectangle = new Cesium.Rectangle.fromDegrees(119.406965,35.178182,120.595063,35.81685);
    scene.camera.setView({
        destination: rectangle,
        orientation : {
            heading : 0,
            pitch : Cesium.Math.toRadians(-75),
            roll :0.0
        }
    });
    //专题图面状
    mapApp.cesiumMap.viewer.scene=scene.addS3MTilesLayerByScp('http://47.104.21.51:8090/iserver/services/3D-QDMap/rest/realspace/datas/datamian/config',{name: 'zhuantiPolyGon'});

    var datamian=mapApp.cesiumMap.viewer.scene.layers.find("zhuantiPolyGon");
    if(datamian) {
        datamian._visible = true;
    }
    //mapApp.cesiumMap.viewer.scene=scene.open('http://47.104.21.51:8090/iserver/services/3D-QDMap/rest/realspace');
}


/**
 * 关闭专题图线状
 */
function closeZhuantiMapLine() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    var data2=mapApp.cesiumMap.viewer.scene.layers.find("zhuantiLine");

    if(data2){
        data2._visible=false;
    }
}

/**
 * 关闭专题图面状
 */

function closeZhuantiMapPolyGon() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    var datamian=mapApp.cesiumMap.viewer.scene.layers.find("zhuantiPolyGon");
    if(datamian) {
        datamian._visible = false;
    }
}


/**
 * 加载外部导入模型服务
 * 石油大学测试数据
 */
function addModelUrl1() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    // var url='http://123.234.218.59:8092/iserver/services/3D-DongQu0520/rest/realspace';//加载模型的url
    mapApp.cesiumMap.viewer.scene=scene.addS3MTilesLayerByScp('http://123.234.218.59:8092/iserver/services/3D-DongQu0520/rest/realspace/datas/shuiyoudaxue_1@%E7%9F%B3%E6%B2%B9%E5%A4%A7%E5%AD%A6/config',{name : '石油大学'});
    var shiyoudaxue=mapApp.cesiumMap.viewer.scene.layers.find("石油大学");
    if(shiyoudaxue) {
        shiyoudaxue._visible = true;
    }

    scene.camera.flyTo({
        destination: Cesium.Cartesian3
            .fromDegrees(120.167, 35.945,1500)
    });



}

/**
 * 清除服务模型
 * 石油大学
 */
function clearModelUrl1() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    if(mapApp.cesiumMap.viewer.scene.layers.find("石油大学")){
        var shiyoudaxue=mapApp.cesiumMap.viewer.scene.layers.find("石油大学");
        shiyoudaxue._visible = false;
    }

}

/**
 * 加载外部导入模型服务
 * 卓亭广场测试数据
 */
function addModelUrl2() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    // var url='http://123.234.218.59:8092/iserver/services/3D-DongQu0520/rest/realspace';//加载模型的url
    mapApp.cesiumMap.viewer.scene=scene.addS3MTilesLayerByScp('http://123.234.218.59:8092/iserver/services/3D-DongQu0520/rest/realspace/datas/zhuotingguangchang_1@%E5%8D%93%E4%BA%AD%E5%B9%BF%E5%9C%BA/config',{name : '卓亭广场'});
    var zhuotingguangchang=mapApp.cesiumMap.viewer.scene.layers.find("卓亭广场");
    if(zhuotingguangchang) {
        zhuotingguangchang._visible = true;
    }

    scene.camera.flyTo({
        destination: Cesium.Cartesian3
            .fromDegrees(120.170, 35.986,1500)
    });
}

/**
 * 清除服务模型
 * 卓亭广场
 */
function clearModelUrl2() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    if(mapApp.cesiumMap.viewer.scene.layers.find("卓亭广场")){
        var zhuotingguangchang=mapApp.cesiumMap.viewer.scene.layers.find("卓亭广场");
        zhuotingguangchang._visible = false;
    }

}


/**
 * 加载外部模型文件
 * 这里为了方便删除
 * 给定测试id=11方便删除
 */
function addModelFile() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    //var height = scene.getHeight(116.467372827584, 39.9101343043);

    var model={
        id: 11,
        name: '测试大楼',
        position: Cesium.Cartesian3.fromDegrees(120.0996511,35.889081744, 100),
        model: {uri: './js/ExportModel.gltf'}
    };

    viewer.entities.add(model);

    scene.camera.flyTo({
        destination: Cesium.Cartesian3
            .fromDegrees(120.100, 35.8895,1500)
    });

    //下面是另外一种加载方法
    // var height = scene.getHeight(116.467372827584, 39.9101343043);
    // var model = scene.primitives.add(Cesium.Model.fromGltf({
    //     url: 'Cesium_Ground.gltf',
    //     //以下这些信息也均可在entity中设置
    //     color: Cesium.Color.fromAlpha(Cesium.Color.RED, parseFloat(0.5)),//模型颜色，透明度
    //     silhouetteColor: Cesium.Color.fromAlpha(Cesium.Color.GREEN, parseFloat(0.5)),//轮廓线
    //     colorBlendMode: Cesium.ColorBlendMode.MIX,//模型样式['Highlight', 'Replace', 'Mix']
    //     modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(
    //         Cesium.Cartesian3.fromDegrees(116.467372827584, 39.9101343043, height)
    //     ),
    //     minimumPixelSize: 256, // 最小的缩放尺寸，256个像素，就是一个瓦片的尺寸。
    //     maxiumScale: 1 // 最大的缩放倍数
    // }));
}

/**
 * 根据模型id清除外部模型文件
 *  @param id: 导入外部文件的id
 */
function clearModelFile() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    viewer.entities.removeById(11);


}




/**
 * 数字景区撒点
 * 星光岛id： 5
 * 金沙滩id： 6
 */
function pointOfSmartScenePoint() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    //调整相机视角
    var rectangle = new Cesium.Rectangle.fromDegrees(119.357413,36.124866,120.703308,35.512322);
    scene.camera.flyTo({
        destination: rectangle
    });

    viewer.entities.add({
        id: 5,
        name : '星光岛',
        position : Cesium.Cartesian3.fromDegrees(120.106,35.895,50),
        billboard :{
            image : './img/location4.png',
            width:30,
            height:40
        }
    });

    viewer.entities.add({
        id: 6,
        name : '金沙滩',
        position : Cesium.Cartesian3.fromDegrees(120.2416512,35.959081745,50),
        billboard :{
            image : './img/location4.png',
            width:30,
            height:40
        }
    });

    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

    handler.setInputAction(function (movement) {

        var pick = viewer.scene.pick(movement.position);

        if (Cesium.defined(pick) && (pick.id.id === 5)) {
            //相机飞到星光岛
            var rectangle = new Cesium.Rectangle.fromDegrees(120.090,35.902, 120.118,35.887);
            scene.camera.flyTo({
                destination: rectangle
            });

            window.top.showBigData("星光岛");
        }else if(Cesium.defined(pick) && (pick.id.id === 6)){
            //相机飞到金沙滩
            var rectangle = new Cesium.Rectangle.fromDegrees(120.241,35.962, 120.242,35.954);
            scene.camera.flyTo({
                destination: rectangle
            });
            window.top.showBigData("金沙滩");
        }

    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);


}


/**
 * 清除数字景区的撒点(根据id)
 */
function clearSmartScenePoint() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    viewer.entities.removeById(5);
    viewer.entities.removeById(6);


}

/**
 * s3m倾斜数据属性查询
 */
function queryS3M() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);

    handler.setInputAction(function (movement) {
        var pick = viewer.scene.pick(movement.position);

        if (Cesium.defined(pick) && (pick.id.id === 11)) {
           alert(mapApp.cesiumMap.viewer.entities._entities._array[0].name);

        }
        var name=scene.layers.getSelectedLayer()._name;
        alert(name);
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

}

/**
 * 全图
 */
function AllView() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    //调整相机视角
    var rectangle = new Cesium.Rectangle.fromDegrees(119.357413,36.044866,120.753308,35.382322);
    scene.camera.flyTo({
        destination: rectangle,
        orientation : {
            heading : 0,
            pitch : Cesium.Math.toRadians(-85),
            roll :0.0
        }
    });

}
/**
 * 景区全图
 */
function AllView2() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    //调整相机视角
    var rectangle = new Cesium.Rectangle.fromDegrees(119.357413,36.124866,120.703308,35.512322);
    scene.camera.flyTo({
        destination: rectangle
    });
}

/**
 * 场景出图
 */
function sceneToPhoto() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    var promise = scene.outputSceneToFile();
    Cesium.when(promise, function (base64data) {
        download(base64data);
    });
}

/**
 * 下载图片(场景出图)
 */
function download(base64data) {
    var image = new Image();
    image.src = base64data;
    image.onload = function() {
        var canvas = convertImageToCanvas(image);
        url = canvas.toDataURL("image/jpeg");
        var a = document.createElement('a');
        var event = new MouseEvent('click');
        a.download = (new Date()).getTime() + ".jpg"; // 指定下载图片的名称
        a.href = url;
        a.dispatchEvent(event); // 触发超链接的点击事件
    }
}

/**
 * 根据图片生成画布(场景出图)
 */
function convertImageToCanvas(image) {

    var canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
    canvas.getContext("3d").drawImage(image, 0, 0);
    return canvas;

}







/**
 * 分屏操作（涉及了我自己写的DIV）
 */
function fenPing(value) {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    // $('#viewportType').change(function(){
    //     var value = $(this).val();
        switch (value) {
            case "NONE":
                $('#pannel').hide();
                break;
            default:
                $('#pannel').show();
                $('#b_one,#b_two,#g_one,#g_two').show();
                $('#b_three,#g_three,#b_four,#g_four').hide();
                break;
        }
        scene.multiViewportMode = Cesium.MultiViewportMode[value];
    // });

    // $('#ground .squaredTwo').click(function(evt){
    //     var value = Number($(this).attr('data'));
    //     value -= 1;
    //     var chk = $(evt.target).prev();
    //     var isChecked = chk[0].checked;
    //     ground.setVisibleInViewport(value, isChecked);
    // });
    // $('#building .squaredTwo').click(function(evt){
    //     var value = Number($(this).attr('data'));
    //     value -= 1;
    //     var chk = $(evt.target).prev();
    //     var isChecked = chk[0].checked;
    //     build.setVisibleInViewport(value, isChecked);
    // });

}

/**
 * 位置拾取
 */
function pickLocation() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    //新建一个变量
    mapApp.cesiumMap.pickLocation=new Object();

    mapApp.cesiumMap.pickLocation = new Cesium.ScreenSpaceEventHandler(scene.canvas); //创建位置拾取对象

    var handler= mapApp.cesiumMap.pickLocation;

    //设置鼠标左键单击回调事件
    //e是标准浏览器传递进去的事件参数，
    handler.setInputAction(function (e){
        //首先移除之前添加的点
        viewer.entities.removeAll();
        //获取点击位置笛卡尔坐标
        var position = scene.pickPosition(e.position);
        //将笛卡尔坐标转化为经纬度坐标
        var cartographic = Cesium.Cartographic.fromCartesian(position);
        var longitude = Cesium.Math.toDegrees(cartographic.longitude);
        var latitude = Cesium.Math.toDegrees(cartographic.latitude);
        var height = cartographic.height;
        if(height < 0) {
            height = 0;
        }
        //创建弹出框信息
        var entity=new Cesium.Entity({
            //向用户显示的可读名称，名称可不必唯一。
            name : "位置信息",
            description : createDescription(Cesium, [longitude, latitude, height])
        });
        viewer.selectedEntity=entity;

        //在点击位置添加对应点
        viewer.entities.add(new Cesium.Entity({
            point : new Cesium.PointGraphics({
                color : new Cesium.Color(1, 1, 0),
                pixelSize : 10,
                outlineColor : new Cesium.Color(0, 1, 1)
            }),
            position : Cesium.Cartesian3.fromDegrees(longitude, latitude , height + 0.5)
        }));
        //表示鼠标左键单击事件。
    },Cesium.ScreenSpaceEventType.LEFT_CLICK );

}

/**
 * 创建描述位置的对话框
 */
function createDescription(Cesium,properties){
    var simpleStyleIdentifiers = ['经度','纬度','高度'];
    var html = '';
    for ( var key in properties) {
        if (properties.hasOwnProperty(key)) {
            if (simpleStyleIdentifiers.indexOf(key) !== -1) {
                continue;
            }
            var value = properties[key];
            if (Cesium.defined(value) && value !== '') {
                html += '<tr><td>' + simpleStyleIdentifiers[key] + '</td><td>' + value + '</td></tr>';
            }
        }
    }
    if (html.length > 0) {
        html = '<table class="zebra"><tbody>' + html + '</tbody></table>';
    }
    return html;
}

/**
 * 清除拾取位置图层
 */
function clearPickLocation() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    var handler= mapApp.cesiumMap.pickLocation;
    handler && handler.destroy();
    viewer.entities.removeAll();

}








/**
 * 测距
 */
function measureDis() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    //新建一个变量
    mapApp.cesiumMap.measureF=new Object();
    //初始化测距对象
    mapApp.cesiumMap.measureF.measureDis=new Cesium.MeasureHandler(viewer,Cesium.MeasureMode.Distance);
    var handlerDis= mapApp.cesiumMap.measureF.measureDis;
    //激活
    handlerDis && handlerDis.activate();
    //注册测距功能事件
    handlerDis.measureEvt.addEventListener(function (result) {
        var dis = Number(result.distance);
        var distance = dis > 1000 ? (dis/1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';
        handlerDis.disLabel.text = '距离:' + distance;

    });
    handlerDis.activeEvt.addEventListener(function(isActive){
        if(isActive == true){
            viewer.enableCursorStyle = false;
            viewer._element.style.cursor = '';
            $('body').removeClass('measureCur').addClass('measureCur');
        }
        else{
            viewer.enableCursorStyle = true;
            $('body').removeClass('measureCur');
        }
    });
     //mapApp.cesiumMap.measureF.measureDis=null;
}

/**
 * 清除测距图层
 */
function clearDis() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    var handlerDis=mapApp.cesiumMap.measureF.measureDis;
    handlerDis  && handlerDis.clear();


}

/**
 * 测面
 */
function measureAre() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    //新建一个变量
    mapApp.cesiumMap.measureF=new Object();
    mapApp.cesiumMap.measureF.measureAre=new Cesium.MeasureHandler(viewer,Cesium.MeasureMode.Area);

    var handlerArea=mapApp.cesiumMap.measureF.measureAre;

    //激活
    handlerArea.activate();
    handlerArea.measureEvt.addEventListener(function(result){
        var mj = Number(result.area);
        var area = mj > 1000000 ? (mj/1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'
        handlerArea.areaLabel.text = '面积:' + area;
    });
    handlerArea.activeEvt.addEventListener(function(isActive){
        if(isActive == true){
            viewer.enableCursorStyle = false;
            viewer._element.style.cursor = '';
            $('body').removeClass('measureCur').addClass('measureCur');
        }
        else{
            viewer.enableCursorStyle = true;
            $('body').removeClass('measureCur');
        }
    });

    // mapApp.cesiumMap.measureF.measureAre=null;
}


/**
 * 清除测面图层
 */
function clearAre() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    var handlerArea=mapApp.cesiumMap.measureF.measureAre;
    handlerArea  && handlerArea.clear();


}

/**
 * 测高
 */
function measureHei() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;
    //新建一个变量
    mapApp.cesiumMap.measureF=new Object();
    mapApp.cesiumMap.measureF.measureHei=new Cesium.MeasureHandler(viewer,Cesium.MeasureMode.DVH);

    var handlerHeight=mapApp.cesiumMap.measureF.measureHei;
    //激活
    handlerHeight.activate();
    handlerHeight.measureEvt.addEventListener(function(result){
        var distance = result.distance > 1000 ? (result.distance/1000).toFixed(2) + 'km' : result.distance + 'm';
        var vHeight = result.verticalHeight > 1000 ? (result.verticalHeight/1000).toFixed(2) + 'km' : result.verticalHeight + 'm';
        var hDistance = result.horizontalDistance > 1000 ? (result.horizontalDistance/1000).toFixed(2) + 'km' : result.horizontalDistance + 'm';
        handlerHeight.disLabel.text = '空间距离:' + distance;
        handlerHeight.vLabel.text = '垂直高度:' + vHeight;
        handlerHeight.hLabel.text = '水平距离:' + hDistance;
    });
    handlerHeight.activeEvt.addEventListener(function(isActive){
        if(isActive == true){
            viewer.enableCursorStyle = false;
            viewer._element.style.cursor = '';
            $('body').removeClass('measureCur').addClass('measureCur');
        }
        else{
            viewer.enableCursorStyle = true;
            $('body').removeClass('measureCur');
        }
    });

    // mapApp.cesiumMap.measureF.measureHei=null;
}


/**
 * 清除测高图层
 */
function clearHei() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    var handlerHeight=mapApp.cesiumMap.measureF.measureHei;
    handlerHeight  && handlerHeight.clear();


}


/**
 * 飞行
 */
function flyLine() {
    var viewer=mapApp.cesiumMap.viewer;
    var scene = viewer.scene;

    scene.globe.depthTestAgainstTerrain = false;
    var routes = new Cesium.RouteCollection(viewer.entities);
    //添加fpf飞行文件，fpf由SuperMap iDesktop生成
    var fpfUrl = './SampleData/fpf/newxgdFlyLine.fpf';
    routes.fromFile(fpfUrl);
    //初始化飞行管理
    var flyManager = new Cesium.FlyManager({
        scene: scene,
        routes: routes
    });
    //注册站点到达事件
    flyManager.stopArrived.addEventListener(function (routeStop) {
        routeStop.waitTime = 0.1; // 在每个站点处停留1s
    });
    flyManager.readyPromise.then(function () { // 飞行路线就绪
        var currentRoute = flyManager.currentRoute;
        currentRoute.isLineVisible = true;
        $("#show-line").addClass("alert-checked");
        currentRoute.isStopVisible = true;
        $("#show-stop").addClass("alert-checked");
        //生成飞行文件中的所有站点列表
        var allStops = flyManager.getAllRouteStops();
        var menu = document.getElementById('stopList');
        for (var i = 0, j = allStops.length; i < j; i++) {
            var option = document.createElement('option');
            option.innerHTML = "站点 " + (i + 1);
            option.value = allStops[i].index;
            menu.appendChild(option);
        }

        $('#stopList').change(function () { //注册站点切换事件
            flyManager && flyManager.stop();
            var index = parseInt($(this).val()); // 站点的索引
            var route = flyManager.currentRoute;
            var stop = route.get(index);
            flyManager.currentStopIndex = index;
            flyManager.viewToStop(stop);
        });

        $('#play').click(function () {
            $(this).hide();
            $('#pause').show();
            flyManager && flyManager.play();
        });
        $('#pause').click(function () {
            $(this).hide();
            $('#play').show();
            flyManager && flyManager.pause();
        });
        $('#stop').click(function () {
            $('#pause').hide();
            $('#play').show();
            flyManager && flyManager.stop();
        });

        $('#show-line').unbind();
        $('#show-line').click(function(){
            if ($(this).hasClass("alert-checked")) {
                $(this).removeClass("alert-checked");
                currentRoute.isLineVisible = false;
            } else {
                $(this).addClass("alert-checked");
                currentRoute.isLineVisible = true;
            }
        });

        $('#show-stop').unbind();
        $('#show-stop').click(function(){
            if ($(this).hasClass("alert-checked")) {
                $(this).removeClass("alert-checked");
                currentRoute.isStopVisible = false;
            } else {
                $(this).addClass("alert-checked");
                currentRoute.isStopVisible = true;
            }
        });

        $('#loadingbar').remove();
        
        window.clearflyLine = function () {
            flyManager && flyManager.stop();
            $("#show-line").removeClass("alert-checked");
            currentRoute.isLineVisible = false;
            $("#show-stop").removeClass("alert-checked");
            currentRoute.isStopVisible = false;
        }
    });


}











